<template>
    <div>
        <basic-container>
            <div class="app-container">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
                    <el-form-item label="日期查询">
                        <el-date-picker v-model="value" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="产品SN码">
                        <el-input v-model="queryParams.sequence" placeholder="请输入产品SN码" clearable @clear="getList" />
                    </el-form-item>
                    <!-- <el-form-item label="物料总码">
                     
                        <template>
                            <el-select v-model="queryParams.codes" clearable filterable remote reserve-keyword
                                placeholder="请输入物料总码" :remote-method="remoteMethod" :loading="loading">
                                <el-option v-for="item in options" :key="item.value" :label="item.codes"
                                    :value="item.codes">
                                </el-option>
                            </el-select>
                        </template>
                    </el-form-item> -->
                    <el-form-item label="物料子码">
                        <el-select v-model="queryParams.serial" clearable filterable remote reserve-keyword
                            placeholder="请输入物料子码" :remote-method="remotesubcode" :loading="loading">
                            <el-option v-for="item in subcodeList" :key="item.value" :label="item.serial"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-download" size="mini"
                            @click="handleExport">导出</el-button>

                    </el-col>
                </el-row>
                <el-table v-loading="loading" :data="menuList" row-key="course_id" :default-expand-all="isExpandAll"
                    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" ref="table">
                    <el-table-column type="expand">

                        <template slot-scope="scope">
                            <el-table :data="scope.row.serials" style="width: 100%">
                                <el-table-column prop="facility_name" label="设备名称" width="180">
                                </el-table-column>
                                <el-table-column prop="name" label="物料名称" width="180">
                                </el-table-column>
                                <el-table-column prop="codes" label="物料总码" width="180">
                                </el-table-column>
                                <el-table-column prop="serial" label="物料子码">
                                </el-table-column>
                            </el-table>
                            <!-- <el-form label-position="left" inline class="demo-table-expand">
                                <div>
                                    <el-form-item label="物料总码">
                                        <span v-for="(item, index) in scope.row.serials" :key="index"
                                            style="display: inline-block;width: 100%; overflow-x: scroll;">{{
                    item[0] }}</span>
                                    </el-form-item>
                                </div>
                                <el-form-item label="物料子码">
                                    <span v-for="(item, index) in scope.row.serials" :key="index"
                                        style="display: inline-block; width: 100%; overflow-x: scroll;">{{
                    item[1] }}</span>
                                </el-form-item>
                            </el-form> -->
                        </template>
                    </el-table-column>
                    <el-table-column prop="orders" label="订单编号"></el-table-column>
                    <el-table-column prop="aircraft" label="设备名称" align="center"></el-table-column>
                    <el-table-column prop="name" label="工单名称" align="center"></el-table-column>
                    <el-table-column prop="sequence" label="产品SN码" align="center"></el-table-column>
                    <el-table-column prop="start" label="加工时间" align="center"></el-table-column>
                    <el-table-column prop="finish" label="结束时间" align="center"></el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index"
                    :limit.sync="queryParams.size" @pagination="getList" />
                <!-- 添加或修改菜单对话框 -->
                <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body
                    :close-on-click-modal="false">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                        <el-row>
                            <el-col :span="19">
                                <el-form-item label="设备名称" prop="name">
                                    <el-input v-model="form.name" placeholder="请输入工站名称" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="设备编号" prop="serials">
                                    <el-input v-model="form.serials" placeholder="请输入工站编号" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="设备工站" prop="station">
                                    <el-input v-model="form.station" placeholder="请输入设备工站" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="设备存放地址" prop="workplace">
                                    <el-input v-model="form.workplace" placeholder="请输入设备存放地址" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="设备添购日期" prop="time">
                                    <el-date-picker v-model="form.time" value-format="yyyy-MM-dd" type="date"
                                        placeholder="选择日期" style="width: 100%;">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="19">
                                <el-form-item label="设备有效日期" prop="expired">
                                    <el-date-picker v-model="form.expired" value-format="yyyy-MM-dd" type="date"
                                        placeholder="选择日期" style="width: 100%;">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="submitForm">确 定</el-button>
                        <el-button @click="cancel">取 消</el-button>
                    </div>
                </el-dialog>
            </div>
        </basic-container>
    </div>
</template>

<script>
import {
    getSerials,
    serialsCodes,
    serialSerial,
    // downloadoutput
} from "@/api/serials";
import { mapGetters } from "vuex";

export default {
    data() {
        return {
            // 遮罩层

            loading: false,
            // 显示搜索条件
            showSearch: true,
            subcodeList: [],
            value1: [],
            total: 1,
            inMess: [],
            menuList: [],
            roleOptions: [],
            dataMuen: [
                {
                    value: 0,
                    label: "是",
                },
                {
                    value: 1,
                    label: "否",
                },
            ],
            options: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部折叠
            isExpandAll: false,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                start: null,
                finish: null,
                name: null,
                codes: null,
                serial: null,
                sequence: null,
                index: 1,
                facility_id: null,
                size: 10,
            },
            value: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "设备名称不能为空", trigger: "blur" },
                ],
                serials: [
                    { required: true, message: "设备编号不能为空", trigger: "blur" },
                ],
                station: [
                    { required: true, message: "设备工站不能为空", trigger: "blur" },
                ],
                workplace: [
                    { required: true, message: "设备地址不能为空", trigger: "blur" },
                ],
                time: [
                    { required: true, message: "设备添购时间不能为空", trigger: "blur" },
                ],
                expired: [
                    { required: true, message: "设备有效日期不能为空", trigger: "blur" },
                ],
            },
        };
    },
    created() {


    },
    computed: {
        ...mapGetters(["userInfo", "menu"]),
    },
    mounted() {
        function formatDate(date) {
            const year = date.getFullYear();
            const month = (date.getMonth() + 1).toString().padStart(2, '0');
            const day = date.getDate().toString().padStart(2, '0');
            return `${year}-${month}-${day}`;
        }
        const today = new Date();
        const lastMonth = new Date(today);
        lastMonth.setMonth(today.getMonth() - 1);
        const formattedToday = formatDate(today);
        const formattedLastMonth = formatDate(lastMonth);
        this.queryParams.start = formattedLastMonth
        this.queryParams.finish = formattedToday
        this.value = [formattedLastMonth, formattedToday]
        this.getList();
    },
    methods: {

        onPhoneInput(event) {
            let value = event;
            // 使用正则表达式过滤非数字字符
            value = value.replace(/\D/g, "");
            // 更新输入框的值
            this.form.phone = value;
        },
        /** 查询菜单列表 */
        getList() {
            this.loading = true;
            getSerials(this.queryParams).then((response) => {

                this.menuList = response.data.data;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.getList();
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                facility_id: null,
                workplace: null,
                serials: null,
                name: null,
                station: null,
                time: null,
                expired: null,
            };
        },
        remoteMethod(query) {
            if (!query) {
                return
            } else {
                let quye = {
                    codes: query
                }
                serialsCodes(quye).then(res => {

                    this.options = res.data.data
                })
            }
        },
        remotesubcode(query) {
            if (!query) {
                return
            } else {
                let quye = {
                    serial: query
                }
                serialSerial(quye).then(res => {

                    this.subcodeList = res.data.data
                })
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.start = this.value == null ? null : this.value.length > 0 ? this.value[0] : null
            this.queryParams.finish = this.value == null ? null : this.value.length > 0 ? this.value[1] : null
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                name: "",
                index: 1,
                size: 10,
                serials: "",
                sequence: null,
            };
            this.getList();
        },
        /** 新增按钮操作 */

        handleExport() {
            this.$confirm('是否将数据导出为EXcel文档？').then(() => {
                const params = new URLSearchParams();
                this.value == null ? '' : params.append('start', this.value[0]);
                this.value == null ? '' : params.append('finish', this.value[1]);
                this.queryParams.sequence == null ? '' : params.append('sequence', this.queryParams.sequence);
                   const url = new URL('http://192.168.10.252:8080/api/report/serials/output');
                
                url.search = params.toString();
                window.location.href = url.toString();
            })
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.form = row;
            this.open = true;
            this.title = "修改工站";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.title == "新增设备") {
                        (this.form).then(() => {
                            this.$message({
                                message: "添加成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    } else {
                        (this.form).then(() => {
                            this.$message({
                                message: "修改成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    }
                }
            });
        },

        /** 删除按钮操作 */
        handleDelete(row) {

            this.$confirm('是否确认删除名称为"' + row.name + '"的设备？')
                .then(() => {
                    this.form = row;
                    (this.form).then(() => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        this.open = false;
                        this.getList();
                        this.reset();
                    });
                })
                .catch(() => { });
        },
    },
};
</script>
